package com.stylefeng.guns.modular.channel.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.modular.channel.service.IAppDeviceService;
import com.stylefeng.guns.modular.channel.service.IAppUserService;
import com.stylefeng.guns.modular.channel.service.IChannelService;
import com.stylefeng.guns.modular.channel.service.ILoanOrderService;
import com.stylefeng.guns.modular.system.model.Channel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;


import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 统计子渠道控制器
 *
 * @author fengshuonan
 * @Date 2018-08-27 19:33:33
 */
@Controller
@RequestMapping("/countChannel")
public class CountChannelController extends BaseController {

    private String PREFIX = "/channel/count/channel/";

    @Autowired
    private IChannelService channelService;

    @Autowired
    private IAppDeviceService deviceService;

    @Autowired
    private IAppUserService appUserService;

    @Autowired
    private ILoanOrderService loanOrderService;

    private Long channelId;

    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index(Long channelId) {
        this.channelId = channelId;
        return PREFIX + "channel.html";
    }


    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(Date beginDate, Date endDate) {

//        beginDate = new Date();
//        endDate = new Date();

        EntityWrapper<Channel> channelEntityWrapper = new EntityWrapper<>();
        channelEntityWrapper.where(channelId != null, "super_channel_id={0}", channelId);
//        channelEntityWrapper.like("channel_name", channelName);
        List<Map<String, Object>> mapList = channelService.selectMaps(channelEntityWrapper);

        int totalActiveCount = 0, totalRegisterCount = 0, totalRequestCount = 0, totalBargainCount = 0;
        BigDecimal totalBargainAmount = new BigDecimal(0.00);

        for (Map<String, Object> channelMap: mapList) {

            Long channelId = (Long) channelMap.get("id");

            EntityWrapper wrapper = new EntityWrapper();
            wrapper.between(beginDate != null && endDate != null,"create_time", beginDate, endDate);//设置查询时间范围
            wrapper.where("channel_id={0}", channelId);
            int activeCount = deviceService.selectCount(wrapper);
            int registerCount = appUserService.selectCount(wrapper);

            int requestCount = loanOrderService.selectCountRequest(channelId, beginDate, endDate);
            int bargainCount = loanOrderService.selectCountBargain(channelId, beginDate, endDate);
            BigDecimal bargainAmount = loanOrderService.selectCountBargainAmount(channelId, beginDate, endDate);

            channelMap.put("status", StatusEnum.desc((Integer) channelMap.get("isEnabled")));
            channelMap.put("activeCount", activeCount);
            channelMap.put("registerCount", registerCount);
            channelMap.put("requestCount", requestCount);
            channelMap.put("bargainCount", bargainCount);
            channelMap.put("bargainAmount", bargainAmount);

            totalActiveCount = totalActiveCount + activeCount;
            totalRegisterCount = totalRegisterCount + registerCount;
            totalRequestCount = totalRequestCount + requestCount;
            totalBargainCount = totalBargainCount + bargainCount;
            totalBargainAmount =  totalBargainAmount.add(bargainAmount);

        }
        HashMap<String, Object> countMap = new HashMap<>();
        countMap.put("activeCount", totalActiveCount);
        countMap.put("registerCount", totalRegisterCount);
        countMap.put("requestCount", totalRequestCount);
        countMap.put("bargainCount", totalBargainCount);
        countMap.put("bargainAmount", totalBargainAmount);
        mapList.add(0, countMap);
        return mapList;
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{appUserId}")
    @ResponseBody
    public Object detail(@PathVariable("appUserId") Integer appUserId) {
        return appUserService.selectById(appUserId);
    }
}
